//
//  LWLifeCommonCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/5/22.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWLifeCommonCtrl.h"
#import "SelectImgUtils.h"

@interface LWLifeCommonCtrl ()<SelectImgUtilsDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIImageView *projectImage;
@property (weak, nonatomic) IBOutlet UILabel *selectImgLabel;
@property (weak, nonatomic) IBOutlet UITextField *titleField;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UITextView *contentTextView;

@property (nonatomic, copy) NSString *projectImgUrl;


@property (nonatomic,weak)UIView *currentEditView;
@property (nonatomic,assign)CGFloat moveDis;//移动距离
@property (nonatomic,assign)CGFloat keyboardHeight;//键盘高度，如果为0，代表当前不是编辑模式


@end

@implementation LWLifeCommonCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"生活百科"];
    [self.contentTextView setCornerRadius:0 borderWidth:1 borderColor:RGBColor(204, 204, 204)];
    
    //添加键盘弹出与隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}


- (IBAction)textFieldBeginEditing:(UITextField *)textField{
    NSLog(@"textViewShouldBeginEditing");
    self.currentEditView = textField;
    if (self.keyboardHeight!=0) {
        [self adjustLocationWithKeyboardHeight:self.keyboardHeight];
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    NSLog(@"textViewShouldBeginEditing");
    self.currentEditView = textView;
    if (self.keyboardHeight!=0) {
        [self adjustLocationWithKeyboardHeight:self.keyboardHeight];
    }
    return YES;
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //在键盘显示的时候去调整view
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    self.keyboardHeight = keyboardSize.height;
    [self adjustLocationWithKeyboardHeight:keyboardSize.height];
}

-(void)adjustLocationWithKeyboardHeight:(CGFloat)keyboardHeight{
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect editViewRect = [self.currentEditView convertRect:self.currentEditView.bounds toView:window];
    
    //首页判断键盘有没有遮挡view，如果没有遮挡，那个什么也不做
    CGFloat viewMaxY = CGRectGetMaxY(editViewRect);
    if (viewMaxY+10>SCREENH-keyboardHeight) {
        //代表键盘遮挡了view
        //需要调整uiscrollview的contentoffset移动
        self.moveDis = (viewMaxY+10)-(SCREENH-keyboardHeight);
        [UIView animateWithDuration:0.25 animations:^{
            [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y+self.moveDis)];
        }];
    }
}

-(void)keyboardWillHide:(NSNotification*)notif{
    self.keyboardHeight = 0;
    self.currentEditView = nil;
    if (self.scrollView.contentOffset.y!=0) {
        [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y-self.moveDis)];
    }
    self.moveDis = 0;
}




- (IBAction)uploadBtnClick:(UIButton *)sender {
    if (self.projectImage.image) {
        SelectImgUtils *utils = [SelectImgUtils shareInstance];
        [utils setDelegate:self];
        [utils uploadImg:self.projectImage.image];
    }else{
        [self uploadProject];
    }
}


-(void)uploadProject{
    
    if (!self.projectImage.image) {
        [DialogUtils showTipsWithView:nil String:@"请上传图片"];
        return;
    }
    
    if (ISBLANK(self.titleField.text) ||ISBLANK(self.contentTextView.text)) {
        [DialogUtils showTipsWithView:nil String:@"请将数据填写完整"];
        return;
    }
    
    NSString *url = @"";
    if (self.btn1.isSelected) {
        //安全知识
        url = @"api/safe/lifesave.json";
    }else{
        //娱乐生活
        url = @"api/happylife/lifesave.json";
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"title"] = self.titleField.text;
    params[@"type_img"] = self.projectImgUrl;
    params[@"txt"] = self.contentTextView.text;
    [MeDataTool newLifeCommondWithParam:params url:url success:^(BaseResponse *result) {
        if (result.isSucc) {
            [DialogUtils showTipsWithView:nil String:@"发布成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:nil String:@"发布失败"];
    }];
}

- (IBAction)typeBtnClick:(UIButton *)sender {
    if (sender==self.btn1) {
        [self.btn2 setSelected:NO];
        [self.btn1 setSelected:YES];
    }else{
        [self.btn2 setSelected:YES];
        [self.btn1 setSelected:NO];
    }
}

- (IBAction)uploadImg:(id)sender {
    [self.view endEditing:YES];
    SelectImgUtils *utils = [SelectImgUtils shareInstance];
    [utils setDelegate:self];
    [utils selectImageWithCtrl:self];
}

-(void)selectImgUtilsImg:(UIImage *)img{
    [self.projectImage setImage:img];
    [self.selectImgLabel setHidden:YES];
}

-(void)selectImgUtilsUrl:(NSString *)url{
    //上传成功
    self.projectImgUrl = url;
    [self uploadProject];
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}

- (void)dealloc{
    //移除键盘监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end
